package com.css.upms.biz.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.css.common.core.utils.R;
import com.css.common.core.utils.UUIDUtils;
import com.css.common.security.utils.SecurityUtil;
import com.css.lion.optlog.agent.annotation.OptLog;
import com.css.upms.api.dto.DictDTO;
import com.css.upms.api.entity.SysDict;
import com.css.upms.biz.service.SysDictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDateTime;


/**
 * DictController
 *
 * @author hanyx
 * @date 2019-05-05 13:44:48
 */
@RestController
@AllArgsConstructor
@Api(value = "/dict", tags = {"UPMS-字典管理"})
@RequestMapping("/dict")
public class DictController {

    private final SysDictService dictService;

    /**
     * 简单分页查询
     *
     * @param page    分页对象
     * @param dictDTO 字典管理查询条件
     * @return
     */
    @GetMapping("/page")
    public R<IPage<SysDict>> getDictPage(Page<SysDict> page, DictDTO dictDTO) {
        SysDict dict = new SysDict();
        BeanUtils.copyProperties(dictDTO, dict);
        return new R<>(dictService.getSysDictPage(page, dict));
    }

    /**
     * 通过id查询单条记录
     *
     * @param id
     * @return R
     */
    @GetMapping("/{id}")
    public R<SysDict> getById(@PathVariable("id") String id) {
        if (StringUtils.isBlank(id)) {
            return R.buildError("字典id不能为空");
        }
        return new R<>(dictService.getById(id));
    }

    /**
     * 新增记录
     *
     * @param dictDTO
     * @return R
     */
    @ApiOperation("新增字典管理")
    @OptLog(desc="字典管理",opt="新增部门")
    @PostMapping
    @CacheEvict(value = "dict_details", key = "#DictDTO.type")
//  @PreAuthorize("@pms.hasPermission('biz_dict_add')")
    public R save(@Valid @RequestBody DictDTO dictDTO) {
        SysDict dict = new SysDict();
        BeanUtils.copyProperties(dictDTO, dict);
        dict.setId(UUIDUtils.get32UUID());
        dict.setCreateTime(LocalDateTime.now());
        dict.setCreateBy(SecurityUtil.getUser().getId());
        return R.buildSuccess(dictService.save(dict));
    }

    /**
     * 修改记录
     *
     * @param dict
     * @return R
     */
    @ApiOperation("修改字典管理")
    @OptLog(desc="字典管理",opt="修改部门")
    @PutMapping
    @CacheEvict(value = "dict_details", key = "#sysDict.type")
//  @PreAuthorize("@pms.hasPermission('biz_dict_edit')")
    public R update(@RequestBody SysDict dict) {
        return new R<>(dictService.updateById(dict));
    }

    /**
     * 通过id删除一条记录
     *
     * @param id
     * @return R
     */
    @ApiOperation("删除字典")
    @OptLog(desc="字典管理",opt="删除部门")
    @DeleteMapping("/{id}/{type}")
    @CacheEvict(value = "dict_details", key = "#type")
//  @PreAuthorize("@pms.hasPermission('biz_dict_del')")
    public R removeById(@PathVariable String id, @PathVariable String type) {
        if (StringUtils.isBlank(id)) {
            return R.buildError("字典id不能为空");
        }
        if (StringUtils.isBlank(type)) {
            return R.buildError("字典type不能为空");
        }
        return new R<>(dictService.removeById(id));
    }

    /**
     * 通过字典类型查找字典
     *
     * @param type 类型
     * @return 同类型字典
     */
    @GetMapping("/type/{type}")
    @Cacheable(value = "dict_details", key = "#type")
    public R getDictByType(@PathVariable String type) {
        if (StringUtils.isBlank(type)) {
            return R.buildError("字典type不能为空");
        }
        return new R<>(dictService.list(Wrappers
                .<SysDict>query().lambda()
                .eq(SysDict::getType, type)));
    }

    /**
     * 查询所有字典
     *
     * @return 同类型字典
     */
    @ApiOperation("查询所有字典")
    @GetMapping("/type/list")
    public R getAll() {
        return new R<>(dictService.list());
    }

}
